package servlet;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bean.Staff;
import common.base.BaseServlet;
import common.util.CustomBeanUtils;
import service.AdminService;
import service.impl.AdminServiceImpl;

@WebServlet("/AdminServlet")
public class AdminServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	
	public String loginUi(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		return "/admin/login.jsp";
	}
	
	//登录
	public String login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获得请求参数信息
		Map<String, String[]> parameterMap = request.getParameterMap();	
		//创建Admin对象
		Staff staff = CustomBeanUtils.populate(Staff.class, parameterMap);
		
		//调用service层进行登录判断
		AdminService staffService = new AdminServiceImpl();
		Staff loginStaff = staffService.login(staff);
		if (loginStaff != null) { //登录成功
			request.getSession().setAttribute("staff", loginStaff);
			response.sendRedirect(request.getContextPath() + "/HomeServlet?action=adminUi");
		} else { //登录失败
			response.sendRedirect(request.getContextPath() + "/AdminServlet?action=loginUi");
		}
		return null;
	}
	
}
